package com.sunrise.xdoc.view.employee;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.sunrise.xdoc.entity.contact.NhanVien;
import com.sunrise.xdoc.entity.employee.TraLuong;
import com.sunrise.xdoc.entity.employee.TraLuong.LoaiTraLuong;
import com.sunrise.xdoc.view.AbstractView;

@ManagedBean
@SessionScoped
public class TraLuongView extends AbstractView<TraLuong> {
	private static final long serialVersionUID = 8169082281361244026L;

	private String editTitle;
	private List<NhanVien> nhanViens;

	@Override
	@PostConstruct
	protected void init() {
		super.init();
		nhanViens = getDaoHelper().getDao(NhanVien.class).loadAll();
	}

	public Long getSelectedNhanVienId() {
		if (getEditedEntity() != null && getEditedEntity().getNhanVien() != null) {
			return getEditedEntity().getNhanVien().getId();
		}
		return null;
	}

	public void setSelectedNhanVienId(Long selectedNhanVienId) {
		for (NhanVien nv : nhanViens) {
			if (selectedNhanVienId.equals(nv.getId())) {
				getEditedEntity().setNhanVien(nv);
				return;
			}
		}
		getEditedEntity().setNhanVien(null);
	}

	public void newAction(boolean tatToan) throws InstantiationException, IllegalAccessException {
		super.newAction();
		if (tatToan) {
			getEditedEntity().setLoai(LoaiTraLuong.TAT_TOAN);
			editTitle = "Tất toán";
		} else {
			getEditedEntity().setLoai(LoaiTraLuong.TAM_UNG);
			editTitle = "Tạm ứng";
		}
	}

	@Override
	protected String getDataModelBeanName() {
		return "traLuongDataModel";
	}

	@Override
	protected String getDetailPanelViewId() {
		return "traLuongDialogId";
	}

	public String getEditTitle() {
		return editTitle;
	}

	public void setEditTitle(String editTitle) {
		this.editTitle = editTitle;
	}

	public List<NhanVien> getNhanViens() {
		return nhanViens;
	}

}
